package app.dev.ems.web.wicket.application;

import org.apache.wicket.Application;
import org.apache.wicket.Component;
import org.apache.wicket.Page;
import org.apache.wicket.RestartResponseAtInterceptPageException;
import org.apache.wicket.Session;
import org.apache.wicket.authorization.Action;
import org.apache.wicket.authorization.IAuthorizationStrategy;
import org.apache.wicket.authroles.authentication.AbstractAuthenticatedWebSession;
import org.apache.wicket.authroles.authentication.AuthenticatedWebApplication;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.protocol.https.HttpsConfig;
import org.apache.wicket.protocol.https.HttpsMapper;
import org.apache.wicket.request.component.IRequestableComponent;
import org.apache.wicket.settings.IRequestLoggerSettings;

import app.dev.ems.manager.IEMSManeger;
import app.dev.ems.service.IEMSService;
import app.dev.ems.web.wicket.mapper.WithoutPageVersionInfoMountedMapper;
import app.dev.ems.web.wicket.page.AuthenticatedWebPage;
import app.dev.ems.web.wicket.page.login.LoginPage;
import app.dev.ems.web.wicket.page.user.home.HomePage;
import app.dev.ems.web.wicket.page.user.profile.edit.ProfileEditPage;
import app.dev.ems.web.wicket.page.user.profile.search.ProfileSearchPage;
import app.dev.ems.web.wicket.page.user.profile.view.ProfileViewPage;
import app.dev.ems.web.wicket.session.EMSSession;

import com.google.code.jqwicket.JQComponentOnBeforeRenderListener;
import com.google.code.jqwicket.JQContributionConfig;

public class EMSApplication extends AuthenticatedWebApplication {

	/***************Fields******************/
	
	private IEMSManeger emsManeger;
	private IEMSService emsService;
	
	/***************Fields******************/
	
	/**************Accessors****************/
	
	public IEMSManeger getEmsManeger() {
		return emsManeger;
	}
	
	public void setEmsManeger(IEMSManeger emsManeger) {
		this.emsManeger = emsManeger;
	}	

	public IEMSService getEmsService() {
		return emsService;
	}

	public void setEmsService(IEMSService emsService) {
		this.emsService = emsService;
	}

	public static EMSApplication getInstance() {
		return (EMSApplication) Application.get();
	}	

	public Class<? extends Page> getHomePage() {
		return HomePage.class;
	}

	protected Class<? extends WebPage> getSignInPageClass() {
		return LoginPage.class;
	}

	protected Class<? extends AbstractAuthenticatedWebSession> getWebSessionClass() {
		return EMSSession.class;
	}
	
	/**************Accessors****************/

	@Override
	protected void init() {
		super.init();
		
		JQContributionConfig config = new JQContributionConfig("http://code.jquery.com/jquery-1.7.1.min.js").withJQueryUiJs("http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/jquery-ui.min.js").withJQueryUiCss("http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.16/themes/start/jquery-ui.css");

		getComponentPreOnBeforeRenderListeners().add(new JQComponentOnBeforeRenderListener(config));

		getMarkupSettings().setStripWicketTags(true);

		getSecuritySettings().setAuthorizationStrategy(new IAuthorizationStrategy() {
			public boolean isActionAuthorized(Component component, Action action) {
				return true;
			}

			public <T extends IRequestableComponent> boolean isInstantiationAuthorized(Class<T> componentClass) {
				if (AuthenticatedWebPage.class.isAssignableFrom(componentClass)) {
					if (((EMSSession) Session.get()).isSignedIn()) {
						return true;
					}
					throw new RestartResponseAtInterceptPageException(LoginPage.class);
				}
				return true;
			}
		});

		mount(new WithoutPageVersionInfoMountedMapper("/login", LoginPage.class));
		mount(new WithoutPageVersionInfoMountedMapper("/home", HomePage.class));
		mount(new WithoutPageVersionInfoMountedMapper("/profile/view", ProfileViewPage.class));
		mount(new WithoutPageVersionInfoMountedMapper("/profile/edit", ProfileEditPage.class));
		mount(new WithoutPageVersionInfoMountedMapper("/profile/search", ProfileSearchPage.class));

		setRootRequestMapper(new HttpsMapper(getRootRequestMapper(), new HttpsConfig(8080, 8443)));

		if (!("DEPLOYMENT").equalsIgnoreCase(getConfigurationType().name())) {

			// Get the logger
			IRequestLoggerSettings reqLogger = Application.get().getRequestLoggerSettings();

			// Enable the logger
			reqLogger.setRequestLoggerEnabled(true);

			/**
			 * Set the window of all the requests that is kept in memory for
			 * viewing. Default is 2000, You can set this to 0 then only
			 * Sessions data is recorded (number of request, total time, latest
			 * size)
			 */
			reqLogger.setRequestsWindowSize(3000);
		}
	}
}
